﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ClickToCall.ClickToCallToolbar.Builders
{
    [System.Serializable()]
    public class ContactCollection : System.Collections.Generic.List<Contact>
    {
        private readonly string file = @"{0}\contacts.ctc";
        public ContactCollection()
        {
            System.Configuration.Configuration conf =
                System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal);
            file = string.Format(file, System.IO.Path.GetDirectoryName(conf.FilePath));
        }

        public bool Save()
        {
            System.Xml.Serialization.XmlSerializer ser = null;
            System.IO.FileStream fs = null;
            bool result = true;
            try
            {
                ser = new System.Xml.Serialization.XmlSerializer(typeof(ContactCollection));
                if (System.IO.File.Exists(file))
                    System.IO.File.Delete(file);
                fs = new System.IO.FileStream(file, System.IO.FileMode.Create);
                ser.Serialize(fs, this);
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (ser != null)
                    ser = null;
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
            }
            return result;
        }

        public bool Load()
        {
            System.Xml.Serialization.XmlSerializer ser = null;
            System.IO.FileStream fs = null;
            bool result = true;
            try
            {
                ser = new System.Xml.Serialization.XmlSerializer(typeof(ContactCollection));
                if (System.IO.File.Exists(file))
                {
                    fs = new System.IO.FileStream(file, System.IO.FileMode.Open);
                    ContactCollection cc = (ContactCollection)ser.Deserialize(fs);
                    this.AddRange(cc);
                    cc = null;
                }
                else
                {
                    result = false;
                }
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (ser != null)
                    ser = null;
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
            }
            return result;
        }

        public void Delete()
        {
            try
            {
                if (System.IO.File.Exists(file))
                    System.IO.File.Delete(file);
            }
            catch { }
        }
    }

    [System.Serializable()]
    public class Contact
    {
        public Contact() { }
        public Contact(PhoneNumbers phones, string fullname, string sip, string email)
        {
            this._phones = phones;
            this._fullname = (fullname == null) ? string.Empty : fullname;
            this._sip = (sip == null) ? string.Empty : sip;
            this._email = (email == null) ? string.Empty : email;
        }
        PhoneNumbers _phones = null;

        public PhoneNumbers Phones
        {
            get { return _phones; }
            set { _phones = value; }
        }
        string _fullname = string.Empty;

        public string Fullname
        {
            get { return _fullname; }
            set { _fullname = (value == null) ? string.Empty : value; }
        }
        string _sip = string.Empty;

        public string Sip
        {
            get { return _sip; }
            set { _sip = (value == null) ? string.Empty : value; }
        }

        string _email = string.Empty;
        public string Email
        {
            get { return _email; }
            set { _email = (value == null) ? string.Empty : value; }
        }

        string _matchedNum = string.Empty;
        public string MatchedNumber
        {
            get { return _matchedNum; }
            set { _matchedNum = (value == null) ? string.Empty : value; }
        }
    }

    [System.Serializable()]
    public class PhoneNumbers
    {
        private string _business2TelephoneNumber = string.Empty;

        public string Business2TelephoneNumber
        {
            get { return _business2TelephoneNumber; }
            set { _business2TelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _businessTelephoneNumber = string.Empty;

        public string BusinessTelephoneNumber
        {
            get { return _businessTelephoneNumber; }
            set { _businessTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _callbackTelephoneNumber = string.Empty;

        public string CallbackTelephoneNumber
        {
            get { return _callbackTelephoneNumber; }
            set { _callbackTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _carTelephoneNumber = string.Empty;

        public string CarTelephoneNumber
        {
            get { return _carTelephoneNumber; }
            set { _carTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _companyMainTelephoneNumber = string.Empty;

        public string CompanyMainTelephoneNumber
        {
            get { return _companyMainTelephoneNumber; }
            set { _companyMainTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _homeTelephoneNumber = string.Empty;

        public string HomeTelephoneNumber
        {
            get { return _homeTelephoneNumber; }
            set { _homeTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _mobileTelephoneNumber = string.Empty;

        public string MobileTelephoneNumber
        {
            get { return _mobileTelephoneNumber; }
            set { _mobileTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _otherTelephoneNumber = string.Empty;

        public string OtherTelephoneNumber
        {
            get { return _otherTelephoneNumber; }
            set { _otherTelephoneNumber = (value == null) ? string.Empty : value; }
        }
        private string _primaryTelephoneNumber = string.Empty;

        public string PrimaryTelephoneNumber
        {
            get { return _primaryTelephoneNumber; }
            set { _primaryTelephoneNumber = (value == null) ? string.Empty : value; }
        }
    }
}
